package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageRequestDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.ietf.jgss.Oid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

// 修改后
@Service
@Slf4j
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {
    @Autowired
    private WmSensitiveService wmSensitiveService;

    /**
     * 新增敏感词
     *
     * @param wmSensitive
     */
    @Override
    @Transactional
    public void addSensitive(WmSensitive wmSensitive) {
        WmSensitive one = lambdaQuery().eq(WmSensitive::getSensitives, wmSensitive.getSensitives()).one();
        if (one != null) {
            throw new RuntimeException("敏感词已存在,请重新输入");
        }
        wmSensitiveService.save(wmSensitive);
    }

    /**
     * 修改敏感词
     *
     * @param wmSensitive
     */
    @Override
    @Transactional
    public void updateSensitive(WmSensitive wmSensitive) {
        WmSensitive one = lambdaQuery()
                .eq(WmSensitive::getSensitives, wmSensitive.getSensitives())
                .eq(WmSensitive::getId,wmSensitive.getId())
                .one();
        if (one != null) {
            throw new RuntimeException("敏感词已存在,请重新输入");
        }
        wmSensitiveService.updateById(wmSensitive);
    }

    /**
     * 删除敏感词
     * @param id
     */
    @Override
    @Transactional
    public void deleteById(Integer id) {
        if (id == null){
            return;
        }
        WmSensitive wmSensitive = lambdaQuery()
                .eq(WmSensitive::getId, id)
                .one();
        if (wmSensitive != null){
            wmSensitiveService.removeById(id);
        }
    }

    /**
     * 分页查询敏感词
     * @param dto
     * @return
     */
    @Override
    public ResponseResult pageWmSensitive(PageRequestDto dto) {
        //todo
        // 创建分页对象，设置当前页码和每页大小
        Page<WmSensitive> wmSensitivePage = new Page<>(dto.getPage(),dto.getSize());

        // 执行分页查询，获取敏感词分页数据
        Page<WmSensitive> page = page(wmSensitivePage);

        // 构造分页响应结果，转换分页参数类型并初始化响应对象
        PageResponseResult pageResponseResult = new PageResponseResult(
                Integer.valueOf(page.getCurrent()+""),
                Integer.valueOf(page.getSize()+""),
                Integer.valueOf(page.getTotal()+""));

        // 设置分页数据记录到响应结果中
        pageResponseResult.setData(page.getRecords());

        // 返回封装好的分页响应结果
        return pageResponseResult;
    }
}